//
//  coreChange.swift
//  quickWeather
//
//  Created by Symbol on 14/11/27.
//  Copyright (c) 2014年 Symbol. All rights reserved.
//

//数据交互方法

import Foundation


//获得网络信息的方法,传入天气对象
func getIntWeather(locationCityName : String) -> baiscInfo{
    
    var weatherURL = jointURL(locationCityName) //拼接URL
    
    //获得天气方法
    var weatherNSURL = NSURL(string : weatherURL)
    var data = NSData(contentsOfURL: weatherNSURL!)
    
    //解析json
    var weatherJSON : AnyObject! = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments, error: nil)
    
    var weatherInfo: AnyObject? = weatherJSON.objectForKey("forecast")
    var cityname: AnyObject? = weatherInfo?.objectForKey("city")
    
    var objBaiscInfo = baiscInfo()
    objBaiscInfo.cityname = weatherInfo?.objectForKey("city") as String
    objBaiscInfo.cityid = weatherInfo?.objectForKey("cityid") as String
    objBaiscInfo.guoming = weatherInfo?.objectForKey("index_ag") as String
    objBaiscInfo.date = weatherInfo?.objectForKey("date_y") as String
    objBaiscInfo.duanlian = weatherInfo?.objectForKey("index_cl") as String
    objBaiscInfo.xcar = weatherInfo?.objectForKey("index_xc") as String
    objBaiscInfo.traveling = weatherInfo?.objectForKey("index_tr") as String
    objBaiscInfo.air = weatherInfo?.objectForKey("index_ls") as String
    objBaiscInfo.ultraviolet = weatherInfo?.objectForKey("index_uv") as String
    objBaiscInfo.week = weatherInfo?.objectForKey("week") as String
    
    //温度
    objBaiscInfo.temperature1 = weatherInfo?.objectForKey("temp1") as String
    objBaiscInfo.temperature2 = weatherInfo?.objectForKey("temp2") as String
    objBaiscInfo.temperature3 = weatherInfo?.objectForKey("temp3") as String
    objBaiscInfo.temperature4 = weatherInfo?.objectForKey("temp4") as String
    objBaiscInfo.temperature5 = weatherInfo?.objectForKey("temp5") as String
    objBaiscInfo.temperature6 = weatherInfo?.objectForKey("temp6") as String
    
    //风速
    objBaiscInfo.windSpeed1 = weatherInfo?.objectForKey("fl1") as String
    objBaiscInfo.windSpeed2 = weatherInfo?.objectForKey("fl2") as String
    objBaiscInfo.windSpeed3 = weatherInfo?.objectForKey("fl3") as String
    objBaiscInfo.windSpeed4 = weatherInfo?.objectForKey("fl4") as String
    objBaiscInfo.windSpeed5 = weatherInfo?.objectForKey("fl5") as String
    objBaiscInfo.windSpeed6 = weatherInfo?.objectForKey("fl6") as String
    
    //风向
    objBaiscInfo.dir1 = weatherInfo?.objectForKey("wind1") as String
    objBaiscInfo.dir2 = weatherInfo?.objectForKey("wind2") as String
    objBaiscInfo.dir3 = weatherInfo?.objectForKey("wind3") as String
    objBaiscInfo.dir4 = weatherInfo?.objectForKey("wind4") as String
    objBaiscInfo.dir5 = weatherInfo?.objectForKey("wind5") as String
    objBaiscInfo.dir6 = weatherInfo?.objectForKey("wind6") as String

    //天气描述
    objBaiscInfo.cloud1 = weatherInfo?.objectForKey("weather1") as String
    objBaiscInfo.cloud2 = weatherInfo?.objectForKey("weather2") as String
    objBaiscInfo.cloud3 = weatherInfo?.objectForKey("weather3") as String
    objBaiscInfo.cloud4 = weatherInfo?.objectForKey("weather4") as String
    objBaiscInfo.cloud5 = weatherInfo?.objectForKey("weather5") as String
    objBaiscInfo.cloud6 = weatherInfo?.objectForKey("weather6") as String
    
    
    return objBaiscInfo
}

//获得本地文本数据的方法
func getLocalAddInfo(locationCityName : String) -> String{
    
    var path = NSBundle.mainBundle().pathForResource("cityId", ofType: "plist")
    
    var data = NSDictionary(contentsOfFile: "\(path!)")
    
    var cityId : NSString = data!.objectForKey(locationCityName) as NSString
    
    return cityId
}


//拼接地址的方法，返回地址
func jointURL(locationCityName : String) -> String{
    var cityId = getLocalAddInfo(locationCityName) //城市id
    //原中国气象局方法
    /*
//    var appleId = "4bd16f" //原始appleid 4bd16fba8c4a0d86
//    var Private_Key = "5c2d5ff11493639b969d15d4dcb8eb3c"//原始key 221f01_SmartWeatherAPI_751f63d
    
//    //加密为64位
//    var plainData = Private_Key.dataUsingEncoding(NSUTF8StringEncoding)
//    Private_Key = plainData!.base64EncodedStringWithOptions(.allZeros)
//    //64位加密转换为URL识别
//    Private_Key = Private_Key.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())!
    
    
    //获得本地时间
//    var localdate = NSDateFormatter()
//    localdate.dateFormat = "yyyyMMddHHmmdd"
//    var localdateFormat = localdate.stringFromDate(NSDate())
    
    println(localdateFormat)
*/
    
    var weatherURL = "http://weatherapi.market.xiaomi.com/wtr-v2/weather?cityId=\(cityId)"
    
    println(weatherURL)
    
    return weatherURL
}

//






